package com.example.ymm.modules1.service.impl.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.UserLogin;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.sys.SysMenu;
import com.example.ymm.modules1.entity.my.sys.SysRoleMenu;
import com.example.ymm.modules1.mapper.my.sys.SysMenuMapper;
import com.example.ymm.modules1.query.SysMenuQueryCriteria;
import com.example.ymm.modules1.service.sys.SysMenuService;
import com.example.ymm.modules1.service.sys.SysRoleMenuService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
@Slf4j
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {


    @Autowired
    SysRoleMenuService sysRoleMenuService;

    @Override
    public TableList<SysMenu> list(SysMenuQueryCriteria criteria, YmPageDto pageDto) {
        IPage<SysMenu> page = new Page<SysMenu>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(SysMenu.class, criteria);
        predicate.orderByAsc("sort");
        baseMapper.selectPage(page, predicate);
        TableList<SysMenu> tableList=new TableList(page);
        return tableList;
    }

    public List<SysMenu> queryAll(SysMenuQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(SysMenu.class, criteria);
        predicate.orderByAsc("sort");
        List<SysMenu> result =baseMapper.selectList(predicate);
        return result;
    }


    @Override
    public List<SysMenu> getMenuTreeList(SysMenuQueryCriteria criteria) {
        List<SysMenu> arr = queryAll(criteria);
        List<SysMenu> menus=new ArrayList<>();
        arr.forEach(e->{
            if(UtilValidate.isEmpty(e.getParentId())){
                forEachDeptList(e,arr);
                menus.add(e);
            }
        });
        return menus;
    }
    public void forEachDeptList(SysMenu parent, List<SysMenu> list){

        List<SysMenu> children=new ArrayList<>();
        list.forEach(e->{
            if(parent.getMenuId().equals(e.getParentId())){
                children.add(e);
            }
        });

        if(UtilValidate.isNotEmpty(children)){
            children.sort(new Comparator<SysMenu>() {
                @Override
                public int compare(SysMenu o1, SysMenu o2) {
                    return o1.getSort()-o2.getSort();
                }
            });
            parent.setChildren(children);

            children.forEach(r->{
                forEachDeptList(r,list);
            });
        }

    }

    @Override
    public List<SysMenu> getUserMenus(Integer izMobile) {
        SysMenuQueryCriteria criteria=new SysMenuQueryCriteria();
        criteria.setStatus(1);
        List<SysMenu> result =new ArrayList<>();
        List<SysMenu> menus=new ArrayList<>();
        String userRole = getUserRole();
        if("manage".equals(userRole)){
            menus = queryAll(criteria);
        }else{
            menus = sysRoleMenuService.getMenusByRoleValue(userRole);
        }
//        UserLogin userLogin = getUserLogin();
//        List<String> roleList = userLogin.getRoleList();
//        String roleValue=roleList.get(0);
        for(SysMenu e:menus){
            if("0".equals(e.getParentId())){
                if(UtilValidate.isNotEmpty(izMobile)){
                    if(izMobile.equals(e.getIzMobile())){
                        forEachByRole(e,menus);
                        result.add(e);
                    }
                }else{
                    forEachByRole(e,menus);
                    result.add(e);
                }

            }
        }
        return result;
    }


    public void forEachByRole(SysMenu parent, List<SysMenu> list){

        List<SysMenu> children=new ArrayList<>();
        list.forEach(e->{
            if(parent.getMenuId().equals(e.getParentId())){
                children.add(e);
            }
        });

        if(UtilValidate.isNotEmpty(children)){
            children.sort(new Comparator<SysMenu>() {
                @Override
                public int compare(SysMenu o1, SysMenu o2) {
                    return o1.getSort()-o2.getSort();
                }
            });
            parent.setChildren(children);

            children.forEach(r->{
                forEachDeptList(r,list);
            });
        }

    }
}
